from rest_framework.viewsets import ModelViewSet
from .serializers import GoodSerializer,GoodCategorySerializer,BannerSerializer,IndexCategorySerializer
from .models import *
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import SearchFilter,OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from .filter import GoodsFilter

#商品类别视图类
class GoodCategoryViewSet(ModelViewSet):
    queryset = GoodsCategory.objects.filter(category_type=1)
    serializer_class = GoodCategorySerializer

class GoodsPage(PageNumberPagination):
    page_size = 15
    page_query_param = "page"


#商品视图类
class GoodViewSet(ModelViewSet):
    queryset = Goods.objects.all()
    serializer_class = GoodSerializer
    pagination_class = GoodsPage
    filter_class = GoodsFilter
    filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
    search_fields = ['name', 'goods_brief', 'goods_desc ']
    ordering_fields = ['shop_price', 'sold_num']

#轮播图视图类
class BannerViewSet(ModelViewSet):
    queryset = Banner.objects.all()
    serializer_class = BannerSerializer

#首页商品分类展示
class IndexGoodsViewSet(ModelViewSet):
    queryset = GoodsCategory.objects.filter(is_tab=True, name__in=["生鲜食品", "酒水饮料"])
    serializer_class = IndexCategorySerializer